<?php


namespace App\Services;


use FFMpeg\Coordinate\TimeCode;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class FFMpegUtil
{

    // 获取视频信息
    public static function getVideoInfo($streamPath)
    {
        $ffprobe = app('ffprobe');
        $stream  = $ffprobe->streams($streamPath)->videos()->first();
        return $stream ? $stream->all() : [];
    }

    // 截取
    public static function getCover($streamPath, $fileName, $fromSecond)
    {

        $ffmpeg   = app('ffmpeg');

        try{
            $video    = $ffmpeg->open($streamPath);
            $frame    = $video->frame(TimeCode::fromSeconds($fromSecond)); //提取第几秒的图像
//          $fileName = Str::random(12) . '.jpg';

            $path = "uploads";
            /*if (!is_dir(public_path($path))) {
                mkdir(public_path($path), 0777);
            }*/
            $frame->save(public_path($path.'/'.$fileName));
            return $fileName;
        }catch(\Exception $e){
            return 'img/video_cover.jpg';
        }

    }
}